import res from './res.json'
import { parseJson } from './parse/jsonParse';

// 组件接口 
interface ViewComponent {
  render(): any;
}

class TextView implements ViewComponent {
  constructor(private text: string) {}
  render() {
    // 返回Taro视图
  }
}

class ButtonView implements ViewComponent {
  constructor(private label: string, private onClick: () => void) {}
  render() {
    // 返回Taro视图
  }
}

// 容器组件 
class ViewGroup implements ViewComponent {
  private children: ViewComponent[] = [];
  
  addComponent(component: ViewComponent): void {
    this.children.push(component);
  }

  render() {
    // 返回包含所有子组件的Taro视图
  }
}

// 组件工厂 
class ComponentFactory {
  static createComponent(data: any): ViewComponent {
    switch (data.type) {
      case 'text':
        return new TextView(data.text);
      case 'button':
        return new ButtonView(data.label, data.onClick);
      case 'viewGroup':
        const group = new ViewGroup();
        data.children.forEach(child => {
          group.addComponent(ComponentFactory.createComponent(child));
        });
        return group;
      default:
        throw new Error('Unknown component type');
    }
  }
}

// 视图构建者 (构建者模式)
class ViewBuilder {
  private rootComponent: ViewGroup = new ViewGroup();

  addComponent(component: ViewComponent): ViewBuilder {
    this.rootComponent.addComponent(component);
    return this;
  }

  build(): ViewComponent {
    return this.rootComponent;
  }
}

// 解释器 (解释器模式)
class JSONInterpreter {
  interpret(json: any, builder: ViewBuilder): ViewBuilder {
    if (Array.isArray(json)) {
      json.forEach(item => this.interpret(item, builder));
    } else {
      builder.addComponent(ComponentFactory.createComponent(json));
    }
    return builder;
  }
}

// SDK入口
class ParsingSDK {
  parse(json: any): ViewComponent {
    const builder = new ViewBuilder();
    const interpreter = new JSONInterpreter();
    return interpreter.interpret(json, builder).build();
  }
}

// // 使用SDK
// const sdk = new ParsingSDK();
// const json = {
//   type: 'viewGroup',
//   children: [
//     { type: 'text', text: 'Hello, World!' },
//     { type: 'button', label: 'Click Me', onClick: () => console.log('Clicked!') }
//   ]
// };
// const viewComponent = sdk.parse(json);
// const view = viewComponent.render();


export function test() {
  console.log(parseJson(res.dataMap.data))
}